package com.zkingsoft.actions.admin;

import java.io.File;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import javax.annotation.Resource;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.zkingsoft.actions.common.BaseController;
import com.zkingsoft.anotations.RemoveRequestToken;
import com.zkingsoft.anotations.SaveRequestToken;
import com.zkingsoft.model.CaseStep;
import com.zkingsoft.model.CommonUserCase;
import com.zkingsoft.plugin.pojo.PaginationVO;
import com.zkingsoft.plugin.util.WebUtil;
import com.zkingsoft.pojo.AjaxResult;
import com.zkingsoft.services.CaseStepService;
import com.zkingsoft.services.CommonUserCaseDaoService;


/**
 * @author gaoke
 * @date 2017-02-06 通用用例管理Controller
 */
@Controller
@RequestMapping(value = "admin/commonUserCase")

public class CommonUserCaseController extends BaseController {

	@Resource
	private CommonUserCaseDaoService CommonUserCaseDaoService;
	@Resource
	private CaseStepService caseStepService;
	
	/**
	 * 列表显示
	 */
	@RequestMapping(value = "/showList")
	public @ResponseBody AjaxResult showList(CommonUserCase commonUserCase, PaginationVO pageVo) {
		return showList(CommonUserCaseDaoService, commonUserCase, pageVo);
	}
	
	/**
	 * 分组获取用例的执行历史记录
	 */
	@RequestMapping(value = "/showHistoryList")
	public @ResponseBody AjaxResult showHistoryList(Long caseId) {
		List<List<CaseStep>> row=caseStepService.findHistoryList(caseId);
		return new AjaxResult(AjaxResult.STATUS_OK,null,"",row,null);
	}

	/**
	 * 新增或者修改页面
	 */
	@RequestMapping(value = "/addOrModify")
	@RemoveRequestToken
	public @ResponseBody AjaxResult addOrModify(CommonUserCase commonUserCase) {
		if (commonUserCase.getCaseId() != null) {
			return modify(CommonUserCaseDaoService, commonUserCase, "用例");
		} else {
			commonUserCase.setUserId(getMe().getId());
			commonUserCase.setCreateTime(new Date());
			commonUserCase.setCaseStatus("正常");
			commonUserCase.setSuccess(0);
			commonUserCase.setFail(0);
			return add(CommonUserCaseDaoService, commonUserCase, "用例");
		}		
	}

	/**
	 * 进入修改界面
	 */
	@RequestMapping(value = "/editForm")
	@SaveRequestToken
	public String editForm(Long id) {
		CommonUserCase commonUserCase;
		if (id != null) {
			commonUserCase = CommonUserCaseDaoService.findById(id);
			WebUtil.getRequest().setAttribute("obj", commonUserCase);
		}
		return "admin/commonUserCase-form";
	}
	
	/**
	 * 删除
	 */
	@RequestMapping(value = "/del")
	public @ResponseBody AjaxResult del(String keys) {
		//System.out.println(keys);
		return remove(CommonUserCaseDaoService, keys);
	}
	/**
	 * 删除
	 * @throws DocumentException 
	 */
	@RequestMapping(value = "/importFile")
	public @ResponseBody AjaxResult importFile(String keys) throws DocumentException {
		File file=new File("/Users/admin/Desktop/123.xml"
				);
		
		SAXReader reader = new SAXReader();
		Document doc = reader.read(file);
		Element rows = doc.getRootElement().element("rows");
		Element Record;
		for (Iterator<?> i = rows.elementIterator("row"); i.hasNext();) {
			CommonUserCase userCase=new CommonUserCase();
			Record = (Element) i.next();
			//userCase.setProjId(22L);
			userCase.setUserId(getMe().getId());
			userCase.setCreateTime(new Date());
			userCase.setCaseStatus("正常");
			userCase.setSuccess(0);
			userCase.setFail(0);
			//userCase.setCaseNo(currentService.findMaxNo(userCase.getProjId())+1);
			userCase.setTitle(Record.elementText("title"));
			userCase.setBeforCondition(Record.elementText("precondition"));
			userCase.setGrade(Integer.parseInt(Record.elementText("pri")));
			userCase.setCaseType("功能测试");
			userCase.setUserStage("单元测试阶段");
			//设置步骤
			String [] stepDesc=Record.elementText("stepDesc").split("<br />");
			String [] stepExpect=Record.elementText("stepExpect").split("<br />");
			List<CaseStep> stepList=new ArrayList<>();
			for (int i1=0;i1<stepDesc.length;i1++) {
				CaseStep step=new CaseStep();
				String stepNo=stepExpect[i1].substring(0,stepExpect[i1].indexOf("."));
				String title=stepDesc[i1].substring(stepDesc[i1].indexOf(".")+1);
				String expectation=stepExpect[i1].substring(stepExpect[i1].indexOf(".")+1);
				step.setStepNo(Integer.parseInt(stepNo));
				step.setTitle(title);
				step.setExpectation(expectation);
				stepList.add(step);
			}
			userCase.setStepsNoHistory(stepList);
			CommonUserCaseDaoService.add(userCase);
		}
		return remove(CommonUserCaseDaoService, keys);
	}
			
			

}